package com.tientui.app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.tientui.app.widgets.WGFWebView;

public class AppController {
	MainActivity mActivity;
	WGFWebView mWebView;
	private com.tientui.app.WGFWebViewClient webViewClient;
	
	public AppController( MainActivity activity) {
		
		this.mActivity = activity;
		
		mWebView = (WGFWebView)mActivity.findViewById(R.id.wGFWebView);
		mWebView.loadUrl(AppConfig.URL_HOME);
		
		WebChromeClient webChromeClient  = new WebChromeClient() {
    		@Override
    		public boolean onConsoleMessage(ConsoleMessage cm) {
			  Log.e("WebConsole", cm.message() + " -- From line "
			                         + cm.lineNumber() + " of "
			                         + cm.sourceId() );
			   Log.e("WebConsole", cm.toString());
			    return true;
			}
    		
    		@Override
			public boolean onJsAlert(WebView view, String url, String message, final JsResult result) 
		    {
				final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
				builder.setTitle("Odin")
						.setMessage(message)
						.setPositiveButton(android.R.string.ok,
								new AlertDialog.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										result.confirm();
									}
								}).setCancelable(false).create().show();
				return true;
		    };
		};

		mWebView.setWebChromeClient(webChromeClient);		
		webViewClient = new WGFWebViewClient(mActivity);
		mWebView.setWebViewClient(webViewClient);
		
	}
	
	public void loadUrl(String url) {
		mWebView.loadUrl(url);
	}
	
	public void onMenuSelect(int id) {
		
	}

	public void reload() {
		// TODO Auto-generated method stub
		mWebView.reload();
	}
}
